JiuGuanScene={}
JiuGuanSceneFunc={}
JiuGuanSceneFunc.__index = JiuGuanSceneFunc
setmetatable(JiuGuanScene, JiuGuanSceneFunc)
JiuGuanScene.NodeID = {UpButton=1020}
JiuGuanScene.upgradeCooldown = 0

JiuGuanScene.ColorRate = {[1]=1,[2]=1.5,[3]=2,[4]=3.5,[5]=5}

JiuGuanSceneFunc.getNode = function()
  local l = {}
  setmetatable(l, JiuGuanSceneFunc)
  l.node = CCBReader:load("JiuGuanScene.ccbi")
  print("JiuGuanScene.getNode", l.node)
  JiuGuanScene.instance = l
  l:init() 
  return l 
end

JiuGuanSceneFunc.getScene = function()
  local sc = CCScene:create()
  sc:addChild(JiuGuanScene.getNode().node)
  print("JiuGuanScene.getScene", sc)
  return sc
end

JiuGuanSceneFunc.init = function(self)
  setObjectExtTable(self.node:getChildByTag(1111), self)
  setObjectExtTable(self.node:getChildByTag(1020), self)
  --TypeCast(self.node:getChildByTag(1020), "CCButton"):setEnabled(false)
  if JiuGuanScene.needLoad==nil or JiuGuanScene.needLoad==true then
    --setTouchEnabledAll(self.node, false)
    NetworkRequest.autoFlushBounty("onGetJiuGuanQuestComplete", self) 
    LoadScene:showLoadingScene()
  else
    self:initRenWuLabel()
  end
  JiuGuanScene.needLoad = true 
  --self:initRenWuLabel()
end

JiuGuanSceneFunc.onGetJiuGuanQuestComplete = function(self, data)
  --print("JiuGuanSceneFunc.onGetJiuGuanQuestComplete", data)
  LoadScene:removeLoadingScene()
  --setTouchEnabledAll(self.node, true)
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    setTouchEnabledAll(self.node, true)
  else
    showMessage(res.msg, ccc3(255,255,0))
  end
  self:initRenWuLabel()
end

JiuGuanSceneFunc.initRenWuLabel = function(self)
  local quests = UserData.userinfo.quests
  --self.jiuguanQuests = {}
  --local count = 1
  --for k,v in pairs(quests) do
  for k,v in ipairs(UserData.userinfo.host.store.bounty.b) do
    local quest = Quest:create(v.q) 
    local item = self.node:getChildByTag(1040+k)
    print("JiuGuanSceneFunc.initRenWuLabel", 1040+k, item)
    TypeCast(item:getChildByTag(1010), "CCLabelTTF"):setString(quest.info.title)
    TypeCast(item:getChildByTag(1010), "CCLabelTTF"):setColor(Equip.EquipColorById[v.s])
    local sp = TypeCast(item:getChildByTag(1001), "CCSprite")
    setSpriteImg(sp, string.format("jiuguanimg%d.png", 10000+v.s))
    TypeCast(item:getChildByTag(1011), "CCLabelTTF"):setString(autoEnter(quest.info.desc, 20))
    local xp = JiuGuanScene.ColorRate[v.s]*Player.getLevelData().bountyXp
    TypeCast(item:getChildByTag(1012), "CCLabelTTF"):setString(tostring(math.floor(xp)))
    local cash = JiuGuanScene.ColorRate[v.s]*Player.getLevelData().bountyCash
    TypeCast(item:getChildByTag(1013), "CCLabelTTF"):setString(tostring(math.floor(cash)))
    if k==UserData.userinfo.host.store.bounty.i+1 or v.c==1 then
      TypeCast(item:getChildByTag(1020), "CCButton"):setEnabled(false)
    end 
    setObjectExtTable(item:getChildByTag(1020), self)
    item:getChildByTag(1020):setTag(100000+k)
    --count = count+1
  end

  local c = UserData.userinfo.host.store.bounty.n
  TypeCast(self.node:getChildByTag(1010), "CCLabelTTF"):setString(string.format("%d/20", 20-c))
  self.refreshCD = UserData.userinfo.host.store.bounty.e - TimeManager.getServerTime()
  --self.refreshCD = 5 
  TypeCast(self.node:getChildByTag(1011), "CCLabelTTF"):setString(formatSecond(self.refreshCD))
  self.timerID = CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(JiuGuanSceneFunc.onTimer, 1, false)

  local f = UserData.userinfo.host.store.bounty.f
  if f<2 then
    TypeCast(self.node:getChildByTag(1020), "CCButton"):setTitle(string.format("免费刷新%d次", 2-f))
  else
    TypeCast(self.node:getChildByTag(1020), "CCButton"):setTitle("军饷刷新")
  end
end
JiuGuanSceneFunc.onTimer = function()
  JiuGuanScene.instance.refreshCD = JiuGuanScene.instance.refreshCD - 1
  if JiuGuanScene.instance.refreshCD == 0 then
    --NetworkRequest.autoFlushBounty("onGetJiuGuanQuestComplete", JiuGuanScene.instance) 
    --LoadScene:showLoadingScene()
    --CCDirector:sharedDirector():getScheduler():unscheduleScriptEntry(JiuGuanScene.instance.timerID)
    --MainScene.scene:removeChild(JiuGuanScene.instance.node, true)
    --JiuGuanScene.instance = nil
    JiuGuanScene.instance:reset()
    MainScene.scene:addChild(JiuGuanScene.getNode().node)
  else
    TypeCast(JiuGuanScene.instance.node:getChildByTag(1011), "CCLabelTTF"):setString(formatSecond(JiuGuanScene.instance.refreshCD))
  end 
end

JiuGuanSceneFunc.onAcceptButton = function(self, node, event)
  --setTouchEnabledAll(self.node, false)
  local index = node:getTag() - 100000
  self.acceptedNode = node
  --TypeCast(self.acceptedNode, "CCButton"):setEnabled(false)
  print("JiuGuanSceneFunc.onAcceptButton", qid)
  NetworkRequest.acceptQuest(UserData.userinfo.host.store.bounty.b[index].q, "onAcceptQuestComplete", self, index-1)
  --setTouchEnabledAll(self.node, false)
  LoadScene:showLoadingScene()
end
JiuGuanSceneFunc.onAcceptQuestComplete = function(self, data)
  print("RenWuSceneFunc.onAcceptQuestComplete", data)
  --setTouchEnabledAll(self.node, true)
  local res = cjson.decode(data)
  if res.ret==0 and res.data.result==true then
    NetworkRequest.syncData(res)
    showMessage("接受到新任务", ccc3(0,255,0))
  else
    --TypeCast(self.acceptedNode, "CCButton"):setEnabled(true)
    showMessage(res.msg, ccc3(255,0,0))
  end
  LoadScene:removeLoadingScene()
  JiuGuanScene.needLoad = false 
  self:reset()
  MainScene.scene:addChild(JiuGuanScene.getNode().node)
end
JiuGuanSceneFunc.onRefreshButton = function(self, node, event)
  local f = UserData.userinfo.host.store.bounty.f
  if f<2 then
    NetworkRequest.flushBounty("onRefreshComplete", self)
    LoadScene:showLoadingScene()
    --setTouchEnabledAll(self.node, false)
    --TypeCast(self.node:getChildByTag(1020), "CCButton"):setEnabled(false)
  else
    --NetworkRequest.flushBounty("onRefreshComplete", self)
    local msg = string.format("10军饷刷新当前所有酒馆任务", cost)
    local tmp = Dialog:create("提示", msg, self)
    MainScene.scene:addChild(tmp.node)
    JiuGuanScene.instance:reset()
    --MainScene.scene:removeChild(self.node, true)
    self.dlg = tmp
  end
  --NetworkRequest.autoFlushBounty("onRefreshComplete", self)
end
JiuGuanSceneFunc.onConfirm = function(self, dlg, confirm)
  if confirm==false then
    JiuGuanScene.needLoad = false 
    MainScene.scene:removeChild(dlg.node, true)
    MainScene.scene:addChild(JiuGuanScene.getNode().node)
  else
    --NetworkRequest.resetAndRefreshGeneral(self.jiasuID, self.cost, "onJiaSuComplete", self)
    NetworkRequest.flushBounty("onRefreshComplete", self)
    --setTouchEnabledAll(dlg.node, false)
    LoadScene:showLoadingScene()
  end
end
JiuGuanSceneFunc.onRefreshComplete = function(self, data)
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
  else
    showMessage(res.msg, ccc3(255,0,0))
  end
  if self.dlg~=nil then
    MainScene.scene:removeChild(self.dlg.node, true)
  end
  if JiuGuanScene.instance~=nil then
    JiuGuanScene.instance:reset()
  end
  LoadScene:removeLoadingScene()
  MainScene.scene:addChild(JiuGuanScene.getNode().node)
end

JiuGuanSceneFunc.reset = function(self)
  CCDirector:sharedDirector():getScheduler():unscheduleScriptEntry(self.timerID)
  MainScene.scene:removeChild(self.node, true)
  JiuGuanScene.instance = nil
end

JiuGuanSceneFunc.onClose = function(self, node, event)
  print("JiuGuanSceneFunc.onClose")
  self:reset()
  --CCDirector:sharedDirector():getScheduler():unscheduleScriptEntry(self.timerID)
  --MainScene.scene:removeChild(node:getParent(), true)
  setTouchEnabledAll(MainScene.node, true)
  MainScene.setState()
  --JiuGuanScene.instance = nil
end

